import { consts } from "@/module-00-codebase/pkg-00-const";
import { Bi } from "@/module-00-codebase/pkg-01-container";

export class ReduxCacheManager {
  public read(key?: string): any {
    return Bi.utils.deepCopy(key ? Bi.utils.readValue(storageCache, key) : storageCache);
  }
  public write(key: string, value: any): void {
    Bi.utils.writeValue(storageCache, key, value);
    window.localStorage.setItem(STORAGE_KEY, Bi.dataExchange.serialize(storageCache));
  }
  public clear(): void {
    storageCache = {};
    window.localStorage.removeItem(STORAGE_KEY);
  }
}

export const initializing = async () => {
  const storageOrigin = window.localStorage.getItem(STORAGE_KEY);
  storageCache = storageOrigin ? Bi.dataExchange.parse(storageOrigin) : {};
};

export const dependencies = () => ["DataExchange"];

export const order = () => consts.firstOrder;

let storageCache: any;
const STORAGE_KEY = "CACHE_MANAGER_STORAGE_KEY";
